package com.newInstance;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName NewInstanceStudy
 * @Description 反射的学习
 * @Author csh
 * @Date 2021/11/15 13:50
 */
public class NewInstanceStudy {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //反射创建方式
        Object [] params = {"test"};
        String methodName = "append";
        Object stringObj = Class.forName("java.lang.StringBuilder").newInstance();
        Class[] parameterTypes = new Class[params.length];
        for (int i = 0; i < parameterTypes.length; i++) {
            parameterTypes[i] = Object.class;
        }
        Method concat = stringObj.getClass().getMethod(methodName, parameterTypes);
        Object invoke = concat.invoke(stringObj,params);
        System.out.println("反射:"+invoke);


        //new创建方式
        StringBuilder stringGet = new StringBuilder("test");
        System.out.println("new:"+stringGet);
    }
}
